<template>
  <div style="display: inline-block">
    <div class="auditBtnDiv">
      <el-button v-for="(item,i) in auditBtnData" :key="item.buttonId" size="small" type="primary" @click="clickBtn(item.buttonId, item)">{{item.buttonName}}</el-button>
      <el-button v-for="(item,i) in customBtnList" :key="item.buttonId" size="small" type="primary" @click="clickBtn(item.buttonId, item)">{{item.buttonName}}</el-button>
    </div>
<!--    <el-dialog title="人员选择" :visible.sync="peopleSelectDialog" width="500px" :close-on-click-modal="false" :destroy-on-close="true"-->
<!--               v-if="peopleSelectDialog" top="30px" custom-class="myDialog" :append-to-body="true">-->
<!--      <work-flow-sys-user-com @selectCallBack="doAddSigner"></work-flow-sys-user-com>-->
<!--    </el-dialog>-->
    <work-flow-pass-com
      ref="auditPassCom"
      :busiUserId="curUserId"
      :busiUserName="curUserName"
      :busiOrgId="curOrgId"
      :workitemInsId="workitemInsId"
      :relateData="busiRelateData"
      @auditParamCallBack="doAuditPass"></work-flow-pass-com>
    <work-flow-reject-com
      ref="auditRejectCom"
      :busiUserId="curUserId"
      :busiUserName="curUserName"
      :busiOrgId="curOrgId"
      :workitemInsId="workitemInsId"
      :relateData="busiRelateData"
      @auditParamCallBack="doAuditReject"></work-flow-reject-com>
    <el-dialog title="审核意见" :visible.sync="auditOpinionDialog" width="450px" :close-on-click-modal="false" :destroy-on-close="true"
               v-if="auditOpinionDialog" top="100px" custom-class="myDialog" :append-to-body="true">
      <div>
        <div style="padding-bottom: 15px">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入内容"
            v-model="auditOpinion">
          </el-input>
        </div>
        <div style="display: inline-block; width: 100%;text-align: right;">
          <el-button size="small" type="success" @click="doSaveAuditOpinion()">确定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  // import { workFlowSysUserCom, workFlowPassCom, workFlowRejectCom } from '@/views/workFlowManage/busiWorkFlowDemo/components'
  import workFlowSysUserCom from './workFlowSysUserCom'
  import workFlowPassCom from './workFlowPassCom'
  import workFlowRejectCom from './workFlowRejectCom'
  export default {
    name: 'workFlowAuditButtonCom',
    data() {
      return {
        curUserId: this.$store.state.user.userid,
        curUserName: this.$store.state.user.name,
        curOrgId: this.$store.state.user.orgId,
        loading: false,
        auditBtnData: [],
        peopleSelectDialog: false,
        auditOpinionDialog: false,
        auditOpinion: '',
        optBtnInfo: {}
      }
    },
    props: ['workitemInsId', 'busiRelateData', 'customBtnList'],
    components: {
      workFlowSysUserCom,
      workFlowPassCom,
      workFlowRejectCom
    },
    created() {
      this.getButtonForAudit()
    },
    watch: {
      workitemInsId() {
        this.getButtonForAudit()
      }
    },
    methods: {
      rowClick(row, event, column) {
        // 行单击事件
      },
      cellClick(row, column, cell, event) {
        // 列单击事件
      },
      rowContextmenu(row, column, e) {
        // 右键菜单
        window.event.returnValue = false
      },
      getButtonForAudit() {
        this.auditBtnData = []
        let args = {}
        args.workitemInsId = this.workitemInsId
        this.$store.dispatch('WfProcessIsExistToBeDone', args).then(res => {
          if (res && res.success) {
            let btnData = [{
              buttonId: '1020_doPass',
              buttonName: '同意'
            },
            {
              buttonId: '2010_doReject',
              buttonName: '驳回'
            }]
            this.auditBtnData = btnData
          }
        }).catch((err) => {
          console.log(err)
        })


        // // 启用按钮配置时，使用
        // let args = {}
        // args.workitemInsId = this.workitemInsId
        //
        // this.$store.dispatch('WfBusiProcActAuditBtn', args).then(res => {
        //   this.auditBtnData = res.data
        //   this.loading = false
        // }).catch((err) => {
        //   console.log(err)
        // })
      },
      clickBtn(btnId, btnInfo) {
        this.optBtnInfo = btnInfo
        //
        if (btnId.endsWith('_doPass') || btnId.endsWith('_doReview') || btnId.endsWith('_countersign') || btnId.endsWith('_doRushToSign') ||
            btnId.endsWith('_doLeaderSigning') || btnId.endsWith('_doChairmanSigning') || btnId.endsWith('_doSignAndApprove') || btnId.endsWith('_doFinancialCountersign')) { // 通过、审阅、会签、抢签、请领导签批、请董事长签批、财务部会签
          if (!this.busiRelateData || !(this.busiRelateData instanceof Array)) {
            this.busiRelateData = []
          }
          this.$emit('auditBefore', btnInfo, this.busiRelateData)
          this.$refs.auditPassCom.doAuditInit()
          //
        } else if (btnId.endsWith('_doReject')) { // 驳回到申请人
          this.auditOpinion = ''
          this.auditOpinionDialog = true
          //
        } else if (btnId.endsWith('_doRejectoFree')) { // 驳回到已审核环节
          if (!this.busiRelateData || !(this.busiRelateData instanceof Array)) {
            this.busiRelateData = []
          }
          this.$emit('auditBefore', btnInfo, this.busiRelateData)
          this.$refs.auditRejectCom.doAuditInit()
          //
        // } else if (btnId.endsWith('_doRedSet') || btnId.endsWith('_doEdit') || btnId.endsWith('_doArchive') ||
        //     btnId.endsWith('_doPrint') || btnId.endsWith('_doDistribute') ||
        //     btnId.endsWith('_doPublishGroupPortal')) { // 套红、编辑、归档、答应、分发、分发到集团首页
        //   this.$emit('auditAfter', this.optBtnInfo, {})
        // } else if (btnId.endsWith('_doRead')) { // 已阅
        //   this.auditOpinion = '已阅'
        //   this.auditOpinionDialog = true
        // } else if (btnId.endsWith('_doAddSigner') || btnId.endsWith('_doDocumentCirculation') || btnId.endsWith('_doAuthorizedCountersign')) { // 前加签、公文流转、授权会签
        //   // this.$emit('auditBefore', btnInfo, this.busiRelateData)
        //   this.peopleSelectDialog = true
        } else {
          this.$emit('auditAfter', this.optBtnInfo, {})
        }
      },
      // doAddSigner(input) {
      //   // curUserId
      //   this.peopleSelectDialog = false
      //   let args = {}
      //   args.busiUserId = this.curUserId
      //   args.busiUserName = this.curUserName
      //   args.workitemInsId = this.workitemInsId
      //   args.auditPeopleList = input
      //
      //   // console.log('args', args)
      //   this.$store.dispatch('WfBusiProcAddSigner', args).then(res => {
      //     if (res && res.success) {
      //       this.$emit('auditAfter', this.optBtnInfo, args)
      //     } else {
      //       this.$message({
      //         type: 'danger',
      //         message: '前加签异常!' + res.message
      //       })
      //     }
      //   }).catch((err) => {
      //     console.log(err)
      //   })
      // },
      doAuditPass(input) { // 选择节点通过回调
        //
        this.$emit('auditAfter', this.optBtnInfo, input)
      },
      doAuditReject(input) { // 选择节点驳回回调
        //
        this.$emit('auditAfter', this.optBtnInfo, input)
      },
      doSaveAuditOpinion() {
        let btnId = this.optBtnInfo.buttonId
        if (btnId.endsWith('_doReject')) {
          this.doAuditRejectByDefault()
          //
        } else if (btnId.endsWith('_doRead')) {
          this.doAuditRead()
          //
        }
      },
      doAuditRejectByDefault() {
        //
        let args = {}
        args.busiUserId = this.curUserId
        args.busiUserName = this.curUserName
        args.busiOrgId = this.curOrgId
        args.workitemInsId = this.workitemInsId
        args.desc = this.auditOpinion
        this.$emit('auditAfter', this.optBtnInfo, args)
        this.auditOpinionDialog = false
      },
      doAuditRead() {
        //
        let args = {}
        args.busiUserId = this.curUserId
        args.busiUserName = this.curUserName
        args.busiOrgId = this.curOrgId
        args.workitemInsId = this.workitemInsId
        args.desc = this.auditOpinion
        this.$emit('auditAfter', this.optBtnInfo, args)
        this.auditOpinionDialog = false
      }
    }
  }
</script>
<style scoped>
  .auditBtnDiv {
    height: 45px;
    overflow: auto;
  }
  .auditBtnDiv >>> .el-button {
    margin: 5px 0px 5px 7px;
  }

</style>
